<!--新增资料分类-->
<template>
  <div class="app-container">
    <el-form
      :model="postForm"
      :rules="rules"
      ref="postForm"
      label-width="220px"
      label-position="right"
    >
      <el-form-item label="顺序：" prop="sort">
        <el-input
          min="0"
          type="number"
          v-model="postForm.sort"
          placeholder="请输入顺序"
          style="width: 300px"
        ></el-input>
      </el-form-item>
      <el-form-item label="类别名称：" prop="name">
        <el-input
          v-model="postForm.name"
          :maxlength="20"
          placeholder="请输入类别名称"
          style="width: 300px"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-check" @click="submitForm">保存</el-button>
        <el-button type="info" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { createDatumCat, getOneDatumCat } from "@/api/datum";
import { classAdd } from "@/api/column";
const postForm = {
  sort: "",
  name: "", // 类别名称
  datumCatId: null
};
// const rules = {
//   sort: [{ required: true, message: '请输入顺序号', trigger: 'submitForm' }],
//   sortName: [{ required: true, message: '请输入课程分类名称', trigger: 'submitForm' }]
// }
export default {
  data() {
    return {
      postForm: {
       name:'',
       sort:'',
       classId:''
      },
      rules: {
        sort: [
          { required: true, message: "请输入顺序号", trigger: "submitForm" }
        ],
        name: [
          {
            required: true,
            message: "请输入课程分类名称",
            trigger: "submitForm"
          }
        ]
      }
    };
  },
  mounted() {
    
  },
  created(){
    if (this.$route.query.id || this.$route.query.id == 0) {
        console.log(this.$route.query.id);
        
      this.postForm.classId = this.$route.query.id;
      this.postForm.name = this.$route.query.name;
      this.postForm.sort = this.$route.query.classsort;
    }  
  },
  watch: {},
  methods: {
    saveClass(id) {
      this.postForm.classId = id;
      classAdd(this.postForm).then(res => {
        this.$message({
          type: "success",
          message: res.data.data
        });
        this.$router.go(-1);
      });
    },
    submitForm() {
      if (this.$route.query.id || this.$route.query.id == 0) {
        this.saveClass(this.$route.query.id);
      } else {
        this.saveClass("");
      }

      //   this.$refs.postForm.validate(valid => {
      //     if (valid) {
      //       createDatumCat(this.postForm)
      //         .then(res => {
      //           console.log(res.data);
      //           if (res.data.statusCode == 200) {
      //             console.log(res.data);
      //             this.$message({
      //               type: "success",
      //               message: res.data.data.msg
      //             });
      //             this.close();
      //           } else {
      //             console.log(res.data.msg);
      //             this.$message({
      //               type: "error",
      //               message: res.data.msg
      //             });
      //           }
      //         })
      //         .catch(err => {
      //           console.log(err);
      //         });
      //     } else {
      //       console.log("0");
      //     }
      //   });
    }
  }
};
</script>
<style scoped>
</style>
